package com.pangolin.pangolinim.ui.adapter;

import android.content.Intent;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.lqr.emoji.LQREmotionKit;
import com.lqr.emoji.MoonUtils;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.ui.activity.ShowBigImageActivity;
import com.pangolin.pangolinim.utils.GsonUtil;

import java.util.List;

public class SessionAdapter extends RecyclerView.Adapter<SessionAdapter.ViewHolder>{
    private List<UIMessage> mMessageList;
    private UIContact me;

    public SessionAdapter(List<UIMessage> messageList , UIContact me){
        mMessageList = messageList;
        this.me = me;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout msgSendLayout;
        LinearLayout msgRecvLayout;
        LinearLayout imgSendLayout;
        LinearLayout imgRecvLayout;

        TextView msgSendText;
        TextView msgRecvText;
        ImageView msgSendImg;
        ImageView msgRecvImg;

        public ViewHolder(View view){
            super(view);
            msgSendLayout = (LinearLayout) view.findViewById(R.id.llSendText);
            msgRecvLayout = (LinearLayout) view.findViewById(R.id.llRecvText);
            imgSendLayout = (LinearLayout) view.findViewById(R.id.llSendImg);
            imgRecvLayout = (LinearLayout) view.findViewById(R.id.llRecvImg);

            msgSendText = (TextView) view.findViewById(R.id.tvSendText);
            msgRecvText = (TextView) view.findViewById(R.id.tvRecvText);
            msgSendImg = (ImageView) view.findViewById(R.id.ivSendImg);
            msgRecvImg = (ImageView) view.findViewById(R.id.ivRecvImg);
        }
    }

    @Override
    public SessionAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_session , parent , false);
        final ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(SessionAdapter.ViewHolder holder, int position) {
        UIMessage msg = mMessageList.get(holder.getAbsoluteAdapterPosition());
        if(msg.getType() == UIMessage.MSG_TEXT){
            if(msg.getIo() == UIMessage.MSG_SEND){
                //消息发送
                holder.msgSendLayout.setVisibility(View.VISIBLE);
                holder.msgRecvLayout.setVisibility(View.GONE);
                holder.imgSendLayout.setVisibility(View.GONE);
                holder.imgRecvLayout.setVisibility(View.GONE);
                holder.msgSendText.setText(msg.getMsgtext());
                MoonUtils.identifyFaceExpression(LQREmotionKit.getContext(), holder.msgSendText, msg.getMsgtext(), ImageSpan.ALIGN_BOTTOM);
            }else{
                //消息接收
                holder.msgSendLayout.setVisibility(View.GONE);
                holder.msgRecvLayout.setVisibility(View.VISIBLE);
                holder.imgSendLayout.setVisibility(View.GONE);
                holder.imgRecvLayout.setVisibility(View.GONE);
                holder.msgRecvText.setText(msg.getMsgtext());
                MoonUtils.identifyFaceExpression(LQREmotionKit.getContext(), holder.msgRecvText, msg.getMsgtext(), ImageSpan.ALIGN_BOTTOM);
            }
        }else if(msg.getType() == UIMessage.MSG_IMG){
            if(msg.getIo() == UIMessage.MSG_SEND){
                //图片发送
                holder.msgSendLayout.setVisibility(View.GONE);
                holder.msgRecvLayout.setVisibility(View.GONE);
                holder.imgSendLayout.setVisibility(View.VISIBLE);
                holder.imgRecvLayout.setVisibility(View.GONE);
                if(msg.getBitmap() != null) {
                    holder.msgSendImg.setImageBitmap(msg.getBitmap());
                }
                //点击响应
                holder.msgSendImg.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(view.getContext(), ShowBigImageActivity.class);
                        intent.putExtra("param1",GsonUtil.toJson(me));
                        intent.putExtra("param2",msg.getMsgtext());
                        view.getContext().startActivity(intent);
                    }
                });
            }else{
                //图片接收
                holder.msgSendLayout.setVisibility(View.GONE);
                holder.msgRecvLayout.setVisibility(View.GONE);
                holder.imgSendLayout.setVisibility(View.GONE);
                holder.imgRecvLayout.setVisibility(View.VISIBLE);
                if(msg.getBitmap() != null) {
                    holder.msgRecvImg.setImageBitmap(msg.getBitmap());
                }
                //点击响应
                holder.msgRecvImg.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(view.getContext(), ShowBigImageActivity.class);
                        intent.putExtra("param1",GsonUtil.toJson(me));
                        intent.putExtra("param2",msg.getMsgtext());
                        view.getContext().startActivity(intent);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount(){
        return mMessageList.size();
    }
}
